package com.example.yd202xslzy;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.Toast;

import com.example.yd202xslzy.fragments.CollectionFragment;
import com.example.yd202xslzy.fragments.SearchFragment;
import com.example.yd202xslzy.fragments.ShopFragment;
import com.example.yd202xslzy.fragments.UserFragment;

import java.util.ArrayList;
import java.util.List;
public class HoneActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener, BottomNavigationView.OnNavigationItemSelectedListener {


    //定义ViewPager
    private ViewPager viewPager;
    //定义BottomNavigationView
    private BottomNavigationView bottomNavigationView;
    //定义FragmentPagerAdapter适配器
    private FragmentPagerAdapter adapter;
    //定义Fragment List 用来保存4个Fragment
    private List<Fragment> fragmentList;

    private String UserId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hone);

        // 获取界面中的对象
        viewPager = findViewById(R.id.viewPager);
        bottomNavigationView = findViewById(R.id.bottomNavigationView);

        UserId = getIntent().getStringExtra("user");
        System.out.println("UserId:"+UserId);
        Toast.makeText(getApplicationContext(), "UserId --> "+UserId, Toast.LENGTH_SHORT).show();

        // 定义 4 个 Fragment ，并加⼊入 List 中
        fragmentList = new ArrayList<>();
        fragmentList.add(new ShopFragment());
        fragmentList.add(new CollectionFragment());
        fragmentList.add(new SearchFragment());
        fragmentList.add(new UserFragment());

        //获取FragmentPagerAdapter适配器
        adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            //根据位置返回对应的Fragment   0对应ShopFragment
            @Override
            public Fragment getItem(int i) {
                //根据位置返回对应的Fragment
                //0对应ShopFragment  1对应CollectionFragment
                return fragmentList.get(i);
            }
            //返回Fragment的数量
            @Override
            public int getCount() {
                //返回Fragment数量
                return fragmentList.size();
            }
        };

        //添加适配器
        viewPager.setAdapter(adapter);
        //给ViewPager添加监听器，当前类需要实现OnPageChangeListener接口
        viewPager.addOnPageChangeListener((ViewPager.OnPageChangeListener) this);
        //给 BottomNavigationView 添加监听器器，当前类需要实现 OnNavigationItemSelectedListener 接口
        bottomNavigationView.setOnNavigationItemSelectedListener(this);
        //选中“店铺”导航按钮
        bottomNavigationView.setSelectedItemId(R.id.nav_shop);

    }


    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {

        int id=0;
        switch(i){
            case 0:
                id = R.id.nav_shop;
                break;
            case 1:
                id = R.id.nav_collection;
                break;
            case 2:
                id = R.id.nav_search;
                break;
            case 3:
                id = R.id.nav_user;
                break;
        }
        System.out.println("fanhuiPa:"+id);
        bottomNavigationView.setSelectedItemId(id);
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }



    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

        int id = 0;

        //根据底部导航菜单项的id，指定ViewPager中的Fragment
        switch (menuItem.getItemId()) {

            case R.id.nav_shop:
                id = 0;
                break;
            case R.id.nav_collection:
                id = 1;
                break;
            case R.id.nav_search:
                id = 2;
                break;
            case R.id.nav_user:
                id = 3;
                break;
        }
        System.out.println("fanhuiNa:" + id);
        //指定ViewPager中的Fragment
        viewPager.setCurrentItem(id);
        return true;
    }
}
